package com.example.ssm.controller;

import com.example.ssm.vo.QueryVo;
import com.example.ssm.pojo.BaseDict;
import com.example.ssm.pojo.Customer;
import com.example.ssm.service.IBaseDictService;
import com.example.ssm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    // 客户来源
    @Value("${CUSTOMER_FROM_TYPE}")
    private String CUSTOMER_FROM_TYPE;
    // 客户行业
    @Value("${CUSTOMER_INDUSTRY_TYPE}")
    private String CUSTOMER_INDUSTRY_TYPE;
    // 客户级别
    @Value("${CUSTOMER_LEVEL_TYPE}")
    private String CUSTOMER_LEVEL_TYPE;

    @Autowired
    private IBaseDictService baseDictService;

    @Autowired
    private ICustomerService customerService;

    @RequestMapping("/list")
    public String List(Model m, QueryVo queryVo){

        List<BaseDict> fromType=baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_FROM_TYPE);

        // 所属行业
        List<BaseDict> industryType = this.baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_INDUSTRY_TYPE);
        // 客户级别
        List<BaseDict> levelType = this.baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_LEVEL_TYPE);

        // 把前端页面需要显示的数据放到模型中
        m.addAttribute("fromType", fromType);
        m.addAttribute("industryType", industryType);
        m.addAttribute("levelType", levelType);

//        List<Customer> customers = customerService.list(queryVo);
//        m.addAttribute("customers",customers);

        PageInfo<Customer> pageInfo = customerService.list(queryVo);
        m.addAttribute("pageInfo",pageInfo);
        System.out.println(pageInfo);

        return "customer";
    }


    @RequestMapping("/edit")
    @ResponseBody
    public Customer edit(long id){
        return customerService.queryCustomerById(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(Customer customer){
         customerService.updateCustomerById(customer);
         return  "{\"result\":true}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(long id){
        customerService.deleteCustomerById(id);
        return  "{\"result\":true}";
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(Customer customer){
        customerService.add(customer);
        return  "{\"result\":true}";
    }
}
